package com.dhcc.sds.standard.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.node.ZTreeNode;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.sds.standard.entity.SdsStandDataContrastLogEntity;
import com.dhcc.sds.standard.service.ISdsStandDataContrastLogService;
import com.dhcc.sds.standard.service.ISdsStandDataContrastService;
import com.dhcc.sds.standard.service.ISdsStandDataSourceService;
import com.dhcc.sds.standard.service.ISdsStandDicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 数据源对照
 */
@Controller
@RequestMapping("${sys.backendPath}/sds/sdsstanddatacontrast")
public class SdsStandDataContrastController {

    private String PREFIX = "/backend/sds/sdsstanddatacontrast/";

    @Autowired
    private ISdsStandDicService sdsStandDicService;

    @Autowired
    private ISdsStandDataSourceService standDataSourceService;

    @Autowired
    private ISdsStandDataContrastService sdsStandDataContrastService;

    @Autowired
    private ISdsStandDataContrastLogService sdsStandDataContrastLogService;

    /**
     * 跳转到数据中心字典对照列表首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "sdsstanddatacontrast.html";
    }

    /**
     * 获得数据源tree与标化字典tree
     */
    @RequestMapping("/standardtree")
    @ResponseBody
    public List<ZTreeNode> tree() {
        return sdsStandDicService.tree();
    }

    @RequestMapping(value = "/datatree")
    @ResponseBody
    public List<ZTreeNode> entityTree() {
        return standDataSourceService.getEntityTree();
    }


    /**
     * 加载对照关系
     */
    @RequestMapping("/loadContrastByDataId")
    @ResponseBody
    public R loadContrastByDataId(@RequestParam Long dataId) {
        Map<String, List<Long>> res = this.sdsStandDataContrastService.loadContrastByDataId(dataId);
        return R.ok().put("data", res);
    }

    /**
     * 对照
     */
    @RequestMapping("/contrast")
    @ResponseBody
    public R contrast(@RequestParam Long dataId, @RequestParam Long dicId, @RequestParam Boolean isAdd) {
        Boolean res = this.sdsStandDataContrastService.contrast(dataId, dicId, isAdd);
        return R.ok().put("data", res);
    }

    /**
     * 展示对照日志
     */
    @RequestMapping("/contrastLog")
    @ResponseBody
    public Object list(@RequestParam Map<String, Object> params) {
        Page<SdsStandDataContrastLogEntity> page = new PageFactory<SdsStandDataContrastLogEntity>().defaultPage();
        List<SdsStandDataContrastLogEntity> result = this.sdsStandDataContrastLogService.page(page,params);
        page.setRecords((List<SdsStandDataContrastLogEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }
}
